//
//  DeliveryFV.m
//  ProjectforHDX
//
//  Created by HDX on 16/8/8.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "DeliveryFV.h"
#define DEL_LABEL_HEIGHT 45
#define DEL_LINE_COLOR  [UtilCheck hexStringToColor:@"e1e1df" alpha:1]
#define DEL_FontSize 14

@interface DeliveryFV()<PXAlertViewDelegate,UITextFieldDelegate>



@property(nonatomic,copy) DeliveryFVBlock clickBlock;

/**
 *  物流信息
 */
@property(nonatomic,strong) NSMutableDictionary *logistDict;

/**
 *  物流公司
 */
@property(nonatomic,strong) RZCustomLable *labValue;
/**
 *  云单号
 */
@property(nonatomic,strong) RZCustomTextField *txtValue;

/**
 *  获取的物流公司
 */
@property(nonatomic,strong) NSMutableArray *logisCompanyArray;


/**
 *  当前的物流公司
 */
@property(nonatomic,strong) NSMutableDictionary *currentLogisDict;




@end

@implementation DeliveryFV
/**
 *  创建物流图层
 *
 *  @param frame
 *  @param logis 物流信息
 *
 *  @return
 */
-(id)initDHVWithFram:(CGRect)frame logistics:(NSDictionary*)logis  onClick:(DeliveryFVBlock)success{
    self=[super initWithFrame:frame];
    if(self){
        self.clickBlock=success;
        self.logistDict= [[NSMutableDictionary alloc]initWithDictionary:logis];
        if(!self.logistDict){
            self.logistDict=[[NSMutableDictionary alloc] initWithCapacity:0];
        }
        [self setBackgroundColor:[UIColor whiteColor]];
        if(frame.size.height==0){
            self.height=DEL_LABEL_HEIGHT*2;
        }
        
        //物流公司
        UIView *logisCompanyView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, DEL_LABEL_HEIGHT)];
        [self addSubview:logisCompanyView];
        {
            //项名称
            RZCustomLable *labName=[[RZCustomLable alloc] init];
            [labName setFont:CustomFont(DEL_FontSize)];
            [labName setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
            [logisCompanyView addSubview:labName];
 
            [labName setFrame:CGRectMake(10, 0, 120, DEL_LABEL_HEIGHT)];
            labName.text=[THMangerTool language:@"delivery.com"];
            labName.numberOfLines=1;
            [labName sizeToFit];
            labName.left=10;
            labName.height=DEL_LABEL_HEIGHT;
            
            //项值
            self.labValue=[[RZCustomLable alloc] init];
            [self.labValue setFont:CustomFont(DEL_FontSize)];
            [self.labValue setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
            [logisCompanyView addSubview:self.labValue];
 
            [self.labValue setFrame:CGRectMake(10, 0, logisCompanyView.width-120, DEL_LABEL_HEIGHT)];
            self.labValue.text=[            self.logistDict  objectForKey:@"name"];
            self.labValue.numberOfLines=1;
            [self.labValue sizeToFit];
            self.labValue.left=labName.right+30;
            self.labValue.height=DEL_LABEL_HEIGHT;
            
            //图标
            RZCustomImageView *rightIcon=[[RZCustomImageView alloc] init];
            UIImage *image=BDFILEIMAGENAEM(@"default/iconfont-jinru");
            [rightIcon setImage:image];
            [logisCompanyView addSubview:rightIcon];
            [rightIcon setFrame:CGRectMake(logisCompanyView.width-10-image.size.width,(logisCompanyView.height-image.size.height)/2.f, image.size.width, image.size.height)];
            
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizer:)];
            
            [logisCompanyView addGestureRecognizer:tap];
            
            
        }
        //线
        [self addSoldLine:1 soldcolor:DEL_LINE_COLOR pointA:CGPointMake(0, 0) pointB:CGPointMake(self.width,0)];
        //线
        [self addSoldLine:1 soldcolor:DEL_LINE_COLOR pointA:CGPointMake(0, DEL_LABEL_HEIGHT) pointB:CGPointMake(self.width,DEL_LABEL_HEIGHT)];
        
        //线
        [self addSoldLine:1 soldcolor:DEL_LINE_COLOR pointA:CGPointMake(0, self.height) pointB:CGPointMake(self.width, self.height)];
        
        
        //物流号
        UIView *logisCodeView=[[UIView alloc] initWithFrame:CGRectMake(0, DEL_LABEL_HEIGHT, self.width, DEL_LABEL_HEIGHT)];
 
        [self addSubview:logisCodeView];
        {
            //项名称
            RZCustomLable *labName=[[RZCustomLable alloc] init];
            [labName setFont:CustomFont(DEL_FontSize)];
            [labName setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
            [logisCodeView addSubview:labName];
            
            [labName setFrame:CGRectMake(10, 0, 120, DEL_LABEL_HEIGHT)];
            labName.text=[THMangerTool language:@"delivery.code"];
            labName.numberOfLines=1;
            [labName sizeToFit];
            labName.left=10;
            labName.height=DEL_LABEL_HEIGHT;
            
            //项值
            RZCustomTextField *txtValue=[[RZCustomTextField alloc] init];
            [txtValue setFont:CustomFont(DEL_FontSize)];
            [txtValue setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
            [logisCodeView addSubview:txtValue];
            self.txtValue=txtValue;
            self.txtValue.delegate = self;
            [txtValue setFrame:CGRectMake(labName.right+30, 0, logisCodeView.width-120, DEL_LABEL_HEIGHT)];
            txtValue.text=[            self.logistDict  objectForKey:@"courierID"];
            txtValue.keyboardType = UIKeyboardTypeNumberPad;
            
        }
  
    }
    return self;
}
-(void)tapRecognizer:(UITapGestureRecognizer*)sender{
    [self getLogisCompanyData];
    
}
/**
 *  取值
 */
-(void)getValue{
   [self.logistDict setValue:self.txtValue.text forKey:@"courierID"];
    if(self.clickBlock){
        self.clickBlock(    self.logistDict);
    }
}

//获取物流公司
- (void)getLogisCompanyData{
    [AFHttpTool postJsonDatawithUrl:@"getLogisticsCompany" parameters:nil success:^(id responseObject) {
        
        if ([[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue] == 1) {
            self.logisCompanyArray = [[NSMutableArray alloc]initWithArray:[responseObject objectForKey:@"data"]];
            
            NSMutableArray *actionArray = [[NSMutableArray alloc]init];
            for (NSDictionary *dic in self.logisCompanyArray) {
                [actionArray addObject:dic[@"name"]];
            }
            PXAlertView *AlertView = [[PXAlertView alloc]initAlertWithTitle:[UtilCheck language:@"order.logistics.company"] message:nil cancelTitle:nil otherTitle:nil contentView:nil completion:nil tableview:actionArray];
            AlertView.delegate = self;
            AlertView.mark = 1000;
            [AlertView show];
        }else {
            [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
        }
        
    } fail:^(id responseObject, NSError *error) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
    
}

- (void)getselect:(NSString *)getselect mark:(int)mark row:(int)row{
    if (mark == 1000 && getselect != nil) {
        
        self.logistDict = [[NSMutableDictionary alloc]initWithDictionary: self.logisCompanyArray[row]];
        [self.logistDict setValue:self.txtValue.text forKey:@"courierID"];

        if(self.clickBlock){
            self.clickBlock(    self.logistDict);
        }
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self.logistDict setValue:self.txtValue.text forKey:@"courierID"];
    if(self.clickBlock){
        self.clickBlock(    self.logistDict);
    }

}


@end
